//
//  UserRegistViewController.m
//  Comic
//
//  Created by Andy.He on 16/3/22.
//  Copyright © 2016年 程锦. All rights reserved.
//

#import "UserRegistViewController.h"

@interface UserRegistViewController () 
@property (strong, nonatomic) IBOutlet UITextField *phoneTextField;
@property (strong, nonatomic) IBOutlet UITextField *passportTextField;
@property (strong, nonatomic) IBOutlet UIButton *backBtn;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *textLeftCons;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *textRightCons;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *passportLeftCons;

@property (strong, nonatomic) IBOutlet UIButton *registBtn;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *btnLeftCons;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *btnRightCons;

@property (copy, nonatomic) NSString *username;
@property (copy, nonatomic) NSString *passport;
@end

@implementation UserRegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self btnSetting];
    [self textSetting];
    [self consSetting];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(infoAction)name:UITextFieldTextDidChangeNotification object:nil];
    
}

- (void)btnSetting{
    self.registBtn.backgroundColor = [UIColor colorWithWhite:0.162 alpha:1.000];
    [self.registBtn setTintColor:[UIColor lightGrayColor]];
    [self.registBtn addTarget:self action:@selector(dismissBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.registBtn setTitle:@"注册" forState:UIControlStateNormal];
    self.registBtn.layer.cornerRadius = 10;
    
    [self.backBtn addTarget:self action:@selector(dismissToTop) forControlEvents:UIControlEventTouchUpInside];
}

- (void)dismissToTop{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)dismissBtn{
    [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.username password:self.passport withCompletion:^(NSString *username, NSString *password, EMError *error) {
        NSLog(@"%@",error);
        if (!error) {
            NSLog(@"注册成功");
            [SVProgressHUD showSuccessWithStatus:@"哎呀,你注册成功了"];
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            [SVProgressHUD showErrorWithStatus:@"喂,账号可能重复了，换个再试试!"];
            NSLog(@"error");
        }
    } onQueue:nil];


}

- (void)textSetting{
    self.phoneTextField.layer.cornerRadius = 10;
    self.passportTextField.layer.cornerRadius = 10;
}

- (void)consSetting{
    self.textLeftCons.constant = SCREEN_WIDTH / 4;
    self.textRightCons.constant = SCREEN_WIDTH / 4;
    self.passportLeftCons.constant = SCREEN_WIDTH / 4;
    self.btnLeftCons.constant = self.textLeftCons.constant;
    self.btnRightCons.constant = self.textRightCons.constant;
}

- (void)infoAction{
    self.username = self.phoneTextField.text;
    self.passport = self.passportTextField.text;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.passportTextField resignFirstResponder];
    [self.phoneTextField resignFirstResponder];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
